﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Evoucher.Business.Models;
using Mani.Common;
using System.Web.Security;
using Evoucher.Business;
using System.Configuration;

public partial class package : System.Web.UI.Page
{
    const int IRANCELL = 1;
    const int TALIYA = 2;
    const int HAMRAH = 3;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadPackages();
        }
    }

    private void LoadPackages()
    {
        IVoucherRepository repo = new VoucherRepository();
        gvPackages.DataSource = repo.GetAvailabePackages();
        gvPackages.DataBind();
    }

    protected void gvPackages_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (Membership.GetUser() == null)
        {
            UIHelper.ClientAlert(this, "کاربر گرامی برای خرید بسنه های ویژه باید از اعضای سایت باشید");
            UIHelper.ClientRedirect(this, "login.aspx?ReturnUrl=package.aspx");
            return;
        }

        int id = Convert.ToInt32(e.CommandArgument);

        IVoucherRepository repo = new VoucherRepository();
        PaymentGateway pg = repo.GetGatewayInfo(Convert.ToInt32(ConfigurationManager.AppSettings["DefaultGateway"]));
        aspnet_Users u = repo.GetUserByName(Membership.GetUser().UserName);
        Transaction t = repo.InitPackageTransaction(u, pg, id);

        if (t == null)
        {
            UIHelper.ClientAlert(this, "کاربر گرامی با عرض پوزش بسته انتخابی شما در حال حاضر موجود نمی باشد");
            return;
        }

        repo.persist();

        RemotePost post = new RemotePost(pg.GatewayUrl);
        post.Add("Amount", t.Amount.ToString());
        post.Add("ResNum", t.Id.ToString());
        post.Add("MID", pg.MerchantId);
        post.Add("RedirectURL", ConfigurationManager.AppSettings["RedirectURL"]);

        Session["transaction"] = t;
        post.Post(this);
    }
}